package com.imay.live.adapter.family;

import android.widget.ImageView;

import com.chad.library.adapter.base.BaseMultiItemQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.chad.library.adapter.base.entity.MultiItemEntity;
import com.imay.live.R;
import com.imay.live.activity.family.util.LevelUtil;
import com.imay.live.helper.LevelHelper;
import com.imay.live.manager.FamilyAccountManager;
import com.imay.live.model.FamilyUserModel;
import com.imay.live.model.UserExpsBean;
import com.imay.live.utils.GlideUtil;

import java.util.Map;

/**
 * Created by Administrator on 2017/9/8 0008.
 */

public class FamilyUserAdapter extends BaseMultiItemQuickAdapter<MultiItemEntity, BaseViewHolder> {

    public final static int VIEWTYPE_LINEAR_CONTENT = 0x114;//推荐结果
    public final static int VIEWTYPE_LINEAR_CONTENT_TITLE = 0x115;

    private Map<Integer, UserExpsBean> map;

    int status;

    public static final int OTHERS = 1;
    public static final int ME = 2;

    private int admin5count;
    private int admin1count;
    private int admin0count;

    private String familyNick;

    public FamilyUserAdapter() {
        super(null);
        addItemType(VIEWTYPE_LINEAR_CONTENT, R.layout.item_family_user);
        addItemType(VIEWTYPE_LINEAR_CONTENT_TITLE, R.layout.item_family_user_level_title);
        map = FamilyAccountManager.getInstance().getFamilyUserExps();
    }

    public FamilyUserAdapter(int status) {
        super(null);
        this.status = status;
        addItemType(VIEWTYPE_LINEAR_CONTENT, R.layout.item_family_user);
        addItemType(VIEWTYPE_LINEAR_CONTENT_TITLE, R.layout.item_family_user_level_title);
        map = FamilyAccountManager.getInstance().getFamilyUserExps();
    }

    @Override
    protected void convert(BaseViewHolder helper, MultiItemEntity item) {
        if (item.getItemType() == VIEWTYPE_LINEAR_CONTENT) {
            FamilyUserModel.FamilyUsersBean userBean = (FamilyUserModel.FamilyUsersBean) item;
            ImageView iv_family_accept_head_img = helper.getView(R.id.iv_family_accept_head_img);
            GlideUtil.loadCircleImage(iv_family_accept_head_img,userBean.getImgHead(), R.mipmap.placeholder);
            if (map.containsKey(userBean.getLv())) {
                helper.setVisible(R.id.tv_family_user_live_action, true);
                helper.setText(R.id.tv_family_user_live_action, map.get(userBean.getLv()).getTitle());
                helper.setTextColor(R.id.tv_family_user_live_action, mContext.getResources().getColor(LevelUtil.getLevelColor(userBean.getLv())));
            } else {
                helper.setVisible(R.id.tv_family_user_live_action, false);
            }
            helper.setText(R.id.tv_family_user_live_level, String.valueOf(userBean.getLiveLevel()));
            helper.setText(R.id.tv_family_user_level, String.valueOf(userBean.getUserLevel()));
            helper.setBackgroundRes(R.id.tv_family_user_live_level, LevelHelper.getUserLevelResId(userBean.getUserLevel()));
            helper.setBackgroundRes(R.id.tv_family_user_level, LevelHelper.getLiveLevelResId(userBean.getLiveLevel()));
            helper.setImageResource(R.id.iv_family_accept_sex, LevelHelper.getSexResId(userBean.getSex()));
            helper.setText(R.id.tv_family_accept_family_nick, familyNick);
            helper.setText(R.id.tv_family_accept_name, userBean.getNick());

            int icon = LevelHelper.getVIPIcon(userBean.getVip());
            if (icon == 0) {
                helper.setVisible(R.id.iv_family_accept_vip, false);
            } else {
                helper.setVisible(R.id.iv_family_accept_vip, true);
                helper.setImageResource(R.id.iv_family_accept_vip, icon);
            }

            if (status == ME) {
                helper.setVisible(R.id.iv_family_focus, false);
            } else if (status == OTHERS) {
                if (userBean.isMyFollow()) {
                    helper.setVisible(R.id.iv_family_focus, false);
                } else {
                    helper.setVisible(R.id.iv_family_focus, true);
                    helper.addOnClickListener(R.id.iv_family_focus);
                }
                helper.addOnClickListener(R.id.iv_family_accept_head_img);
            }

            helper.addOnClickListener(R.id.rl_family_user_item);
        } else if (item.getItemType() == VIEWTYPE_LINEAR_CONTENT_TITLE) {
            if (helper.getLayoutPosition() < admin5count) {
                helper.setVisible(R.id.v_family_user_level_title_split, false);
                helper.setText(R.id.tv_family_user_level_title, R.string.str_family_admin_5);
            } else if (helper.getLayoutPosition() < admin5count + admin1count + 1) {
                helper.setVisible(R.id.v_family_user_level_title_split, true);
                helper.setText(R.id.tv_family_user_level_title, R.string.str_family_admin_1);
            } else {
                helper.setVisible(R.id.v_family_user_level_title_split, true);
                helper.setText(R.id.tv_family_user_level_title, R.string.str_family_admin_0);
            }
        }
    }

    public void setAdminCount(int admin5count, int admin1count, int admin0count) {
        this.admin5count = admin5count;
        this.admin1count = admin1count;
        this.admin0count = admin0count;
    }

    public void setFamilyNick(String familyNick) {
        this.familyNick = familyNick;
    }
}
